package com.dt76.smallloanbackenduser.controller.hx;

import com.dt76.smallloanbackenduser.pojo.huxuan.Information;
import com.dt76.smallloanbackenduser.remote.huxuan.SetMatonRemote;
import com.dt76.smallloanbackenduser.utils.Constrants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/huxuan")
public class SetMationController {

    @Autowired
    private SetMatonRemote setMatonRemote;


    @GetMapping("/update")
    public String qryAgreement(Model model){
        System.out.println("查找用户协议内容");
        String agreement = setMatonRemote.qryAgreement();
        System.out.println(agreement+"!!!!!!!!!!");
        //System.out.println(file.getOriginalFilename());
        model.addAttribute("agreement",agreement);
        return "pages/hx/platform-set";
    }

    @RequestMapping("/put")

    public String createArticle(ModelMap map, HttpServletRequest request, Information information){
        Map<String,Object> resultMap = new HashMap<String, Object>();
        try {
            //解析请求中的数据
            MultipartHttpServletRequest mpRequest = (MultipartHttpServletRequest) request;
            MultipartFile file1 = mpRequest.getFile("file1");
            MultipartFile file2 = mpRequest.getFile("file2");
            if(file1.isEmpty() || file2.isEmpty()){
                resultMap.put("result", "请上传图片");
                resultMap.put("data", "");
                resultMap.put("isSuccess", true);
            }else{
            //上传文件的真实名称
            String name1 = file1.getOriginalFilename();
            String name2 = file2.getOriginalFilename();

            System.out.println("----------articleFile-name----------");
//            System.out.println(name1);
//            System.out.println(name2);
           // String filePath = "C:\\TomCat\\apache-tomcat-7.0.92\\Upload\\";
            File dest1 = new File(Constrants.UPLOAD_DISK_DIR + name1);
            File dest2 = new File(Constrants.UPLOAD_DISK_DIR + name2);
            file1.transferTo(dest1);
            file2.transferTo(dest2);
            System.out.println("user"+information.getUser_agreement());

            //图片映射路径
            String filename1 = Constrants.UPLOAD_MAPPING_PATH + name1;
            String filename2 = Constrants.UPLOAD_MAPPING_PATH + name2;
              //  System.out.println(filename1);
              //  System.out.println(filename2);
            information.setLogo(filename1);
            information.setWechar_code(filename2);
            setMatonRemote.update(information);
//               String num = String.valueOf(information.getPhone());
//            information1.setPhone(num);
//                information1.setPlatform_desc(information.getPlatform_desc());
//            information1.setPlatform_name(information.getPlatform_name());
//            information1.setPlatform_title(information.getPlatform_title());
//            information1.setUser_agreement(information.getUser_agreement());
//            information1.setWechar_code(filename2);
//            information1.setWechat_name(information.getWechat_name());
//                System.out.println(information1.toString()+"0o0o0o0o0oo0o0!!!");
//            setMatonRemote.update(information1);
            resultMap.put("result", "操作成功");
            resultMap.put("data", "");
            resultMap.put("isSuccess", false);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "pages/hx/platform-set";
    }

    }



